<template>
<div>
  <elementForm
      ref="eleForm"
      :asterisk="true"
      :inline="true"
      :form="form"
      size="medium"
      @onExceed="onExceed"
      :rules="rules"
      :form-label="formLabel"
      class="eleForm"
      label-position="top"
      label-width="200px"
      @beforeUpload="beforeUpload"
      @upLoad="upLoad">
  </elementForm>
</div>
</template>

<script>
import elementForm from "@/package/elementForm/elementForm";
export default {
name: "upload",
  components: { elementForm },
  data(){
    return{
      form: {
        url: [],
        // https://pic.quanjing.com/vk/ml/QJ6491287097.jpg@!794wsy
        picUrl: '',

      },
      formLabel: [],
      rules: {
        url: [{ required: true, message: '请输入海报地址', trigger: 'blur' }],
        picUrl: [{ required: true, message: '请输入图文消息图片url', trigger: 'blur' }],
      }
    }
  },
  computed: {
    formList () {
      return [
        { prop: 'posterName', label: '海报名称', size: 'small', placeholder: '请输入海报名称', type: 'input', clearable: true },
        { prop: 'title', label: '图文消息标题', size: 'small', placeholder: '请输入图文消息标题', type: 'input', clearable: true },
        { prop: 'description', label: '图文消息描述', size: 'small', placeholder: '请输入图文消息描述', type: 'input', clearable: true },
        {
          prop: 'productType', label: '产品类型', size: 'small', type: 'option',
          option: [
            { value: 1, label: '优企贷' }, { value: 2, label: '优享贷' }, { value: 3, label: '优抵贷' },
            { value: 4, label: '优农贷' }, { value: 5, label: '消费型产品' }, { value: 6, label: '经营型产品' }
          ]
        },
        {
          prop: 'institutionExpireTime',
          label: '请输入合作机构二维码有效时长（小时）',
          size: 'small',
          placeholder: '请输入合作机构二维码有效时长（0-720）',
          type: 'input',
          clearable: true,
          inputType: 'number'
        },
        {
          prop: 'workerExpireTime', label: '请输入客户经理二维码有效时长（小时）', size: 'small', clearable: true,
          placeholder: '请输入客户经理二维码有效时长（0-720）',
          type: 'input', inputType: 'number'
        },
        {
          prop: 'type',
          label: '二维码跳转类型',
          type: 'radio',
          size: 'small',
          radio: [{ val: 1, name: '小程序' }, { val: 2, name: '链接' }]
        },
        {
          prop: 'url',
          label: '海报地址',
          type: 'upload',
          api: '/marketplatform/api/upload/images',
          headers: {Authorization: `Bearer ${localStorage.getItem('token')}`},
          loading: false,
          multiple: true,
          limit: 4,
          'list-type': 'picture-card'
        },
        {
          prop: 'picUrl',
          label: '图文消息图片url',
          type: 'upload',
          api: '/marketplatform/api/upload/images',
          headers: {Authorization: `Bearer ${localStorage.getItem('token')}`},
          loading: false,
          limit: 1,
          'list-type': 'picture-card'
        }
      ]
    }
  },
  mounted () {
    this.formLabel = this.formList
  },
  methods: {
    // 上传
    upLoad (res, name) {
      if (typeof this.form[name] === 'string'){
        this.form[name] = res.data.path
      }else {
        this.form[name].push({ img: res.data.path })
      }
    },
    // 上传之前验证
    beforeUpload (file, res, err) {
      res(file) // 条件成立直接甩出去
    },
    onExceed(){
      this.$message.warning('超出上传文件总数！')
    }
  }
}
</script>

<style lang="scss">
.eleForm{
  .el-form-item{
    width: 100%;
  }
}
</style>
